package com.betterjr.modules.customer;

import static com.betterjr.common.web.ControllerExceptionHandler.exec;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.betterjr.common.web.Servlets;
import com.betterjr.modules.bank.dubbo.interfaces.IBankCityNoService;

/**
 * 
 * @author liuwl
 *
 */
@Controller
@RequestMapping("/Platform/BankAccount")
public class BankAccountController {
    private static final Logger logger = LoggerFactory.getLogger(BankAccountController.class);

    @Reference(interfaceClass = IBankCityNoService.class)
    private IBankCityNoService bankCityNoService;

    @Reference(interfaceClass = ICustMechBankAccountService.class)
    private ICustMechBankAccountService bankAccountService;

    @RequestMapping(value = "/queryBankAccount", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String queryBankAccount(final HttpServletRequest request, final Long custNo) {
        logger.debug("银行帐户信息-银行帐户列表 入参:custNo=" + custNo);
        return exec(() -> bankAccountService.webQueryBankAccount(custNo), "银行帐户信息-银行帐户列表 查询错误", logger);
    }

    /**
     * 根据客户编号查询银行账户，什么都不传时查找平台账户
     * @param custNo
     * @return
     */
    @RequestMapping(value = "/queryBankAccountByCustNo", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String queryBankAccountByCustNo(final HttpServletRequest request, final Long custNo) {
        logger.debug("银行帐户信息-银行帐户列表 入参:custNo=" + custNo);
        return exec(() -> bankAccountService.webQueryBankAccountByCustNo(custNo), "银行帐户信息-银行帐户列表 查询错误", logger);
    }

    /**
     * 
     * @Title: queryAllBankInfo 
     * @Description: TODO(查询所以银行信息) 
     * @param @param request
     * @param @param custNo
     * @param @return 参数说明 
     * @return String 返回类型 
     * @throws 
     * @author chenlq
     * @date 2018年7月7日 下午4:16:21
     */
    @RequestMapping(value = "/queryAllBankInfo", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String queryAllBankInfo(final HttpServletRequest request, String flag, int pageNum,
            int pageSize) {
        return exec(() -> bankAccountService.webQueryAllBankInfo(flag, pageNum, pageSize), "银行帐户信息-银行帐户列表 查询错误",
                logger);
    }

    @RequestMapping(value = "/findBankAccount", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String findBankAccount(final HttpServletRequest request, final Long id) {
        logger.debug("银行帐户信息-银行帐户详情 入参:id=" + id);
        return exec(() -> bankAccountService.webFindBankAccount(id), "银行帐户信息-银行帐户详情 查询错误", logger);
    }

    @RequestMapping(value = "/findBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String findBankAccountTmp(final HttpServletRequest request, final Long id) {
        logger.debug("银行帐户信息-银行帐户流水详情 入参:id=" + id);
        return exec(() -> bankAccountService.webFindBankAccountTmp(id), "银行帐户信息-银行帐户流水详情 查询错误", logger);
    }

    @RequestMapping(value = "/saveBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String saveBankAccountTmp(final HttpServletRequest request, final Long id,
            final String fileList) {
        final Map<String, Object> reqParam = Servlets.getParametersStartingWith(request, "");
        logger.debug("银行帐户信息-银行帐户流水修改 入参:reqParam=" + reqParam + " id=" + id);
        return exec(() -> bankAccountService.webSaveBankAccountTmp(reqParam, id, fileList), "银行帐户信息-银行帐户流水修改 错误",
                logger);
    }

    @RequestMapping(value = "/queryBankAccountKeyAndValue", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String queryBankAccountKeyAndValue(final HttpServletRequest request, final Long custNo) {
        logger.debug("银行帐户信息-银行帐户列表 入参:custNo=" + custNo);
        return exec(() -> bankAccountService.webQueryBankAccountKeyAndValue(custNo), "银行帐户信息-银行帐户列表 查询错误", logger);
    }

    @RequestMapping(value = "/addChangeBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String addChangeBankAccountTmp(final HttpServletRequest request, final String fileList) {
        final Map<String, Object> reqParam = Servlets.getParametersStartingWith(request, "");
        logger.debug("银行帐户信息-临时流水 添加 入参:reqParam=" + reqParam);
        return exec(() -> bankAccountService.webAddChangeBankAccountTmp(reqParam, fileList), "银行帐户信息-临时流水 添加错误",
                logger);
    }

    @RequestMapping(value = "/saveAddCustMechBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String saveAddCustMechBankAccountTmp(final HttpServletRequest request, final String fileList) {
        final Map<String, Object> reqParam = Servlets.getParametersStartingWith(request, "");
        logger.debug("银行帐户信息-临时流水 添加 入参:reqParam=" + reqParam);
        final Object object = reqParam.get("isDefault");
        if (object == null || object.toString().equals("false") || object.toString().equals("0")) {
            reqParam.put("isDefault", "0");
        } else {
            reqParam.put("isDefault", "1");
        }
        return exec(() -> bankAccountService.webSaveAddCustMechBankAccountTmp(reqParam, fileList), "银行帐户信息-临时流水 添加错误",
                logger);
    }

    /**
     *添加银行帐户
     *lcz
     */
    @RequestMapping(value = "/saveAddCustMechBankAccount", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String saveAddCustMechBankAccount(final HttpServletRequest request) {
        final Map<String, Object> reqParam = Servlets.getParametersStartingWith(request, "");
        logger.debug("新增企业银行帐户信息");
        return exec(() -> bankAccountService.webSaveAddCustMechBankAccountInfo(reqParam), "新增企业银行帐户信息失败！", logger);
    }

    /**
     *城市下拉框查询服务
     *lcz
     */
    @RequestMapping(value = "/queryCityForSelect", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String queryCityForSelect(final HttpServletRequest request, final String provinceName) {
        logger.debug("城市下拉框查询服务");
        return exec(() -> bankCityNoService.queryCityForSelect(provinceName), "城市下拉框查询服务失败！", logger);
    }

    /*
     * 查询银行对应的联行号
     */
    @RequestMapping(value = "/queryBankNumber", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String queryBankNumber(final HttpServletRequest request, final String bankFullName) {
        logger.debug("银行所对应的联行号:bankFullName=" + bankFullName);
        return exec(() -> bankAccountService.webQueryBankNumber(bankFullName), "银行对应的联行号查询失败！", logger);
    }

    @RequestMapping(value = "/saveChangeBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String saveChangeBankAccountTmp(final HttpServletRequest request, final String fileList) {
        final Map<String, Object> reqParam = Servlets.getParametersStartingWith(request, "");
        logger.debug("银行帐户信息-临时流水 修改 入参:reqParam=" + reqParam);
        return exec(() -> bankAccountService.webSaveChangeBankAccountTmp(reqParam, fileList), "银行帐户信息-临时流水 修改 错误",
                logger);
    }

    @RequestMapping(value = "/deleteChangeBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String deleteChangeBankAccountTmp(final HttpServletRequest request, final Long refId) {
        logger.debug("银行帐户信息-临时流水 删除 入参:refId=" + refId);
        return exec(() -> bankAccountService.webDeleteChangeBankAccountTmp(refId), "银行帐户信息-临时流水 删除 错误", logger);
    }

    @RequestMapping(value = "/cancelChangeBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String cancelChangeBankAccountTmp(final HttpServletRequest request, final Long id) {
        logger.debug("银行帐户信息-临时流水 作废 入参:id=" + id);
        return exec(() -> bankAccountService.webCancelChangeBankAccountTmp(id), "银行帐户信息-临时流水 作废 错误", logger);
    }

    @RequestMapping(value = "/queryNewChangeBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String queryNewChangeBankAccountTmp(final HttpServletRequest request, final Long custNo) {
        logger.debug("银行帐户信息-临时流水 未使用流水列表 入参:custNo=" + custNo);
        return exec(() -> bankAccountService.webQueryNewChangeBankAccountTmp(custNo), "银行帐户信息-临时流水 未使用流水列表 查询错误",
                logger);
    }

    @RequestMapping(value = "/queryChangeBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String queryChangeBankAccountTmp(final HttpServletRequest request, final Long applyId) {
        logger.debug("银行帐户信息-临时流水 变更流水列表 入参:applyId=" + applyId);
        return exec(() -> bankAccountService.webQueryChangeBankAccountTmp(applyId), "银行帐户信息-临时流水 变更流水列表 查询错误", logger);
    }

    @RequestMapping(value = "/addChangeApply", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String addChangeApply(final HttpServletRequest request, final Long custNo) {
        final Map<String, Object> reqParam = Servlets.getParametersStartingWith(request, "");
        logger.debug("银行帐户信息-变更申请  入参:reqParam=" + reqParam + " custNo=" + custNo);
        return exec(() -> bankAccountService.webAddChangeApply(reqParam, custNo), "银行帐户信息-变更申请  错误", logger);
    }

    @RequestMapping(value = "/saveChangeApply", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String saveChangeApply(final HttpServletRequest request, final Long custNo) {
        final Map<String, Object> reqParam = Servlets.getParametersStartingWith(request, "");
        logger.debug("银行帐户信息-变更申请修改  入参:reqParam=" + reqParam + " custNo=" + custNo);
        return exec(() -> bankAccountService.webAddChangeApply(reqParam, custNo), "银行帐户信息-变更申请修改  错误", logger);
    }

    @RequestMapping(value = "/queryChangeApply", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String queryChangeApply(final HttpServletRequest request, final Long custNo, final int flag,
            final int pageNum, final int pageSize) {
        logger.debug("银行帐户信息-变更列表  入参:custNo=" + custNo);
        return exec(() -> bankAccountService.webQueryChangeApply(custNo, flag, pageNum, pageSize), "银行帐户信息-变更列表查询  错误",
                logger);
    }

    @RequestMapping(value = "/findChangeApply", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String findChangeApply(final HttpServletRequest request, final Long applyId,
            final Long tmpId) {
        logger.debug("银行帐户信息-变更申请  入参: applyId=" + applyId + " tmpId=" + tmpId);
        return exec(() -> bankAccountService.webFindChangeApply(applyId, tmpId), "银行帐户信息-变更申请  错误", logger);
    }

    @RequestMapping(value = "/addInsteadBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String addInsteadBankAccountTmp(final HttpServletRequest request, final Long insteadRecordId,
            final String fileList) {
        final Map<String, Object> reqParam = Servlets.getParametersStartingWith(request, "");
        logger.debug("银行帐户信息-添加代录新增流水  入参:reqParam=" + reqParam + " insteadRecordId=" + insteadRecordId);
        return exec(() -> bankAccountService.webAddInsteadBankAccountTmp(reqParam, insteadRecordId, fileList),
                "银行帐户信息-添加代录新增流水  错误", logger);
    }

    @RequestMapping(value = "/saveInsteadBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String saveInsteadBankAccountTmp(final HttpServletRequest request, final Long insteadRecordId,
            final String fileList) {
        final Map<String, Object> reqParam = Servlets.getParametersStartingWith(request, "");
        logger.debug("银行帐户信息-添加代录修改流水  入参:reqParam=" + reqParam + " insteadRecordId=" + insteadRecordId);
        return exec(() -> bankAccountService.webSaveInsteadBankAccountTmp(reqParam, insteadRecordId, fileList),
                "银行帐户信息-添加代录新增流水  错误", logger);
    }

    @RequestMapping(value = "/deleteInsteadBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String deleteInsteadBankAccountTmp(final HttpServletRequest request, final Long refId,
            final Long insteadRecordId) {
        logger.debug("银行帐户信息-添加代录删除流水  入参:refId=" + refId + " insteadRecordId=" + insteadRecordId);
        return exec(() -> bankAccountService.webDeleteInsteadBankAccountTmp(refId, insteadRecordId),
                "银行帐户信息-添加代录删除流水  错误", logger);
    }

    @RequestMapping(value = "/cancelInsteadBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String cancelInsteadBankAccountTmp(final HttpServletRequest request,
            final Long insteadRecordId, final Long id) {
        logger.debug("银行帐户信息-删除代录流水  入参: id=" + id);
        return exec(() -> bankAccountService.webCancelInsteadBankAccountTmp(id, insteadRecordId), "银行帐户信息-删除代录流水  错误",
                logger);
    }

    @RequestMapping(value = "/queryInsteadBankAccountTmp", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String queryInsteadBankAccountTmp(final HttpServletRequest request,
            final Long insteadRecordId) {
        logger.debug("银行帐户信息-查询代录流水列表  入参:insteadRecordId=" + insteadRecordId);
        return exec(() -> bankAccountService.webQueryInsteadBankAccountTmp(insteadRecordId), "银行帐户信息-查询代录流水列表  错误",
                logger);
    }

    @RequestMapping(value = "/addInsteadRecord", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String addInsteadRecord(final HttpServletRequest request, final Long insteadRecordId) {
        final Map<String, Object> reqParam = Servlets.getParametersStartingWith(request, "");
        logger.debug("银行帐户信息-添加代录记录  入参:reqParam=" + reqParam + " insteadRecordId=" + insteadRecordId);
        return exec(() -> bankAccountService.webAddInsteadRecord(reqParam, insteadRecordId), "银行帐户信息-添加代录记录  错误",
                logger);
    }

    @RequestMapping(value = "/saveInsteadRecord", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String saveInsteadRecord(final HttpServletRequest request, final Long insteadRecordId) {
        final Map<String, Object> reqParam = Servlets.getParametersStartingWith(request, "");
        logger.debug("银行帐户信息-修改代录记录  入参:reqParam=" + reqParam + " insteadRecordId=" + insteadRecordId);
        return exec(() -> bankAccountService.webSaveInsteadRecord(reqParam, insteadRecordId), "银行帐户信息-修改代录记录  错误",
                logger);
    }

    @RequestMapping(value = "/findInsteadRecord", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String findInsteadRecord(final HttpServletRequest request, final Long insteadRecordId) {
        logger.debug("银行帐户信息-代录记录详情  入参: insteadRecordId=" + insteadRecordId);
        return exec(() -> bankAccountService.webFindInsteadRecord(insteadRecordId), "银行帐户信息-代录记录详情  错误", logger);
    }

    @RequestMapping(value = "/findCustMechBankAccount", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String findCustMechBankAccount(final HttpServletRequest request, final String bankAcco) {
        logger.debug("银行帐户信息-代录记录详情  入参: bankAcco=" + bankAcco);
        return exec(() -> bankAccountService.webFindCustMechBankAccount(bankAcco), "银行帐户信息-代录记录详情  错误", logger);
    }

    @RequestMapping(value = "/findBankCode", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String findBankCode(final HttpServletRequest request) {
        final Map anMap = Servlets.getParametersStartingWith(request, "");
        return exec(() -> bankAccountService.webFindSysBankCodeList(anMap), "查询联行号", logger);
    }

    /**
     * 
     * @Title: findBankNoProperty 
     * @Description: TODO(查寻所有银行属性) 
     * @param @param request
     * @param @return 参数说明 
     * @return String 返回类型 
     * @throws 
     * @author chenlq
     * @date 2018年7月12日 下午4:29:35
     */
    @RequestMapping(value = "/findBankNoProperty", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody String findBankNoProperty() {

        return exec(() -> bankAccountService.webQueryAllBankProperty(), "查询银行属性失败！", logger);
    }
}
